package com.ucenter.server.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.system.comm.model.Page;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;
import com.ucenter.server.auth.pojo.ResInfo;
import com.ucenter.server.auth.pojo.RoleInfo;
import com.ucenter.server.auth.pojo.RoleUser;
import com.ucenter.server.auth.service.RoleUserService;
import com.ucenter.server.org.pojo.PositionUser;
import com.ucenter.server.org.service.PositionUserService;
import com.ucenter.server.user.dao.UserInfoDao;
import com.ucenter.server.user.enums.UserInfoStatus;
import com.ucenter.server.user.enums.UserLogType;
import com.ucenter.server.user.pojo.UserInfo;
import com.ucenter.server.user.pojo.UserLog;
import com.ucenter.server.user.pojo.UserWebsite;
import com.ucenter.server.user.service.UserInfoService;
import com.ucenter.server.user.service.UserLogService;
import com.ucenter.server.user.service.UserWebsiteService;
import com.ucenter.server.user.utils.UserInfoUtil;

/**
 * user_info的Service
 * @author autoCode
 * @date 2018-02-06 15:18:19
 * @version V1.0.0
 */
@Component
public class UserInfoServiceImpl implements UserInfoService {

	@Autowired
	private UserInfoDao userInfoDao;
	@Autowired
	private UserWebsiteService userWebsiteService;
	@Autowired
	private RoleUserService roleUserService;
	@Autowired
	private PositionUserService positionUserService;
	@Autowired
	private UserLogService userLogService;
	
	@Override
	public ResponseFrame saveOrUpdate(UserInfo userInfo) {
		ResponseFrame frame = new ResponseFrame();
		if (FrameStringUtil.isNotEmpty(userInfo.getPassword())) {
			String password = UserInfoUtil.encodePassword(userInfo.getPassword());
			userInfo.setPassword(password);
		}
		UserInfo org = null;
		if (FrameStringUtil.isNotEmpty(userInfo.getUserId())) {
			org = get(userInfo.getUserId());
			
		}
		if (org == null) {
			org = userInfoDao.getByUserName(userInfo.getUserName());
			if (org != null && !org.getUserId().equals(userInfo.getUserId())) {
				frame.setCode(-3);
				frame.setMessage("该用户名已经被使用");
				return frame;
			}
			if (FrameStringUtil.isEmpty(userInfo.getCreateUserId())) {
				userInfo.setCreateUserId("0");
			}
			if (FrameStringUtil.isEmpty(userInfo.getUserId())) {
				userInfo.setUserId(FrameNoUtil.uuid());
			}
			userInfoDao.save(userInfo);
		} else {
			userInfoDao.update(userInfo);
		}
		// 处理传入了来源站点编号的情况
		if (FrameStringUtil.isNotEmpty(userInfo.getWebsiteId())) {
			UserWebsite userWebsite = new UserWebsite();
			userWebsite.setUserId(userInfo.getUserId());
			userWebsite.setWebsiteId(userInfo.getWebsiteId());
			userWebsite.setCreateUserId(userInfo.getCreateUserId());
			userWebsiteService.save(userWebsite);
		}
		// 保存角色关系
		if (FrameStringUtil.isNotEmpty(userInfo.getRoleId())) {
			RoleUser roleUser = new RoleUser();
			roleUser.setUserId(userInfo.getUserId());
			roleUser.setRoleId(userInfo.getRoleId());
			roleUser.setCreateUserId(userInfo.getCreateUserId());
			roleUserService.save(roleUser);
		}
		frame.setCode(ResponseCode.SUCC.getCode());
		frame.setBody(userInfo);
		return frame;
	}

	@Override
	public UserInfo get(String userId) {
		return userInfoDao.get(userId);
	}

	@Override
	public ResponseFrame pageQuery(UserInfo userInfo) {
		userInfo.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = userInfoDao.findUserInfoCount(userInfo);
		List<UserInfo> data = null;
		if(total > 0) {
			data = userInfoDao.findUserInfo(userInfo);
			for (UserInfo ui : data) {
				ui.setStatusName(UserInfoStatus.getText(ui.getStatus()));
			}
		}
		Page<UserInfo> page = new Page<UserInfo>(userInfo.getPage(), userInfo.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String userId) {
		ResponseFrame frame = new ResponseFrame();
		userInfoDao.delete(userId);
		
		// 删除关联站点
		userWebsiteService.deleteByUserId(userId);
		// 删除关联角色
		roleUserService.deleteByUserId(userId);
		// 删除关联岗位
		positionUserService.deleteByUserId(userId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public ResponseFrame login(String userName, String password, String websiteId, String ip) {
		ResponseFrame frame = new ResponseFrame();
		UserInfo user = userInfoDao.getByUserName(userName);
		if(user == null) {
			frame.setCode(-2);
			frame.setMessage("用户名或密码错误!");
			return frame;
		}
		if(!user.getPassword().equals(UserInfoUtil.encodePassword(password))) {
			frame.setCode(-2);
			frame.setMessage("用户名或密码错误!");
			return frame;
		}
		// 判断用户是否有权限登录该站点
		UserWebsite uw = userWebsiteService.get(user.getUserId(), websiteId);
		if (uw == null) {
			frame.setCode(-2);
			frame.setMessage("您没有权限登录该站点!");
			return frame;
		}
		user.setPassword(null);
		
		// 获取菜单功能
		List<ResInfo> resList = roleUserService.findResByUidWid(user.getUserId(), websiteId);
		user.setResList(resList);
		// 获取角色列表
		List<RoleInfo> roleList = roleUserService.findRoleByUidWid(user.getUserId(), websiteId);
		user.setRoleList(roleList);
		// 获取部门岗位
		List<PositionUser> positionList = positionUserService.findByUserId(user.getUserId());
		user.setPositionList(positionList);
		
		// 记录登录记录
		userLogService.saveLogin(user.getUserId(), ip);
		
		// 获取近5条用户的日志
		List<UserLog> logList = userLogService.findByUidTypeNum(user.getUserId(), UserLogType.LOGIN.getCode(), 5);
		user.setLogList(logList);
		frame.setBody(user);
		frame.setSucc();
		return frame;
	}
}